#游戏名：丛林训练营
#键盘 w、d、s、a 控制人物 上、右、下、左移动； 按键 j 发射子弹
#by 广辉

# 导包
import pygame, sys
import os
import random
from cls.human import Human
from cls.bullet import Bullet
from cls.enemy import Enemy
from pygame.locals import *
from cls.wordmusic import WordMusic
from pygame.event import Event, post

COLOR = 183, 152, 106
FPS = 30
size = width, height = 1000, 600
enemycount = 8  # 敌人数量
enemymincount = 7  # 敌人最小数量
score = 0  # 得分
WHITE = 255, 255, 255
RED = pygame.color.Color("red")
YELLOW = pygame.color.Color("yellow")
BLUE = pygame.color.Color("blue")
# 判断是否存活
live = True
# 判断是否是初次进入游戏界面
firstS = True
# 首页的背景
indeximage = pygame.image.load("./pic/index.JPG")
#游戏背景
gameimage = pygame.image.load("./pic/bgimage.jpg")
#游戏图片标题
gameico = pygame.image.load("./pic/pic2/human1.png")

pygame.init()  # 初始化
pygame.font.init()  # 字体初始化
pygame.mixer.init()  # 音乐初始化

pygame.mixer.Sound("./music/bgm.wav").play(-1);  # 加载音乐

os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (300, 100)  # 设置窗体位置
screen = pygame.display.set_mode(size)  # 设置窗体大小
pygame.display.set_caption("丛林训练营")  # 设置标题
pygame.display.set_icon(gameico)#设置图片游戏标题
fClock = pygame.time.Clock()  # 用于控制刷新帧率


all_sprites = pygame.sprite.Group()  # 所有的对象都放在这里
bullet_sprites = pygame.sprite.Group()  # 创建敌人组
enemy_sprites = pygame.sprite.Group()  # 创建子弹组
human_sprites = pygame.sprite.Group()  # 玩家组

human = Human()  # 实例化人物

# human_sprites.add(human)
all_sprites.add(human)  # 添加人物


# 实例敌人
def initenemy(size):
    for i in range(size):
        enemy = Enemy((random.randrange(width, width + 1), random.randrange(10, height - 50)))
        enemy_sprites.add(enemy)
        all_sprites.add(enemy)


# 敌人穿过屏幕，敌人就要从起始位置再走一遍
# def returnSP():

# 设置得分以及分数的位置
def showinfo(font_word, font_color, font_position, font_size):
    sys_font = pygame.font.Font("C:\\Windows\\Fonts\\simfang.ttf", font_size)
    score_surface = sys_font.render(font_word, False, font_color)
    if (font_word == "GAME OVER"):
        font_position = (
            font_position[0] - score_surface.get_rect()[2] / 2, font_position[1] - score_surface.get_rect()[3] / 2)
    screen.blit(score_surface, font_position)


initenemy(enemycount)


# def fun(j):  # 用于改变角度等一系列值
#     Human.angle = (j - Human.last) * 90  # 改变转换角度W
#     Human.now = j  # 改变方向的值
#     # human.turn_around(Human.angle)  # 转向


# 模拟打字的样子输出文字
def simul(text, position, textlen, i):
    if i != textlen - 1:
        wm = WordMusic()
        VIDEOEXPOSE, wm.wordmusicplay()  # 播放打字音乐
    showinfo(text, WHITE, position, 20)  # 显示文字内容
    pygame.display.flip()
    if i % 5 == 0:
        pygame.time.wait(600)
    if i == textlen - 1:
        pygame.time.wait(600)


running = True  # 用于判断是否继续主循环
while running:
    for event in pygame.event.get():  # 循环事件对列
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:  # 监听退出按钮
                sys.exit()  # 退出游戏

    if firstS:
        indeximage = pygame.transform.scale(indeximage, (width, height))  # 背景满屏
        screen.blit(indeximage, (0, 0))  # 对其下标
        pygame.display.update()
        text = "士兵，欢迎回到训练营，请尽全力获取更高的分数，准备好，训练即将开始"
        y = 10
        for i in range(len(text)):
            texti = text[i]
            if i % 10 == 0:
                y = 20
            simul(texti, (80 + (i * 27), 200), len(text), i)
        simul(texti, (80 + (i * 27), 200), len(text), i)
        pygame.display.flip()
        pygame.time.wait(2000)
        firstS = False

    if live:
        # returnSP()
        gameimage = pygame.transform.scale(gameimage, (width, height))  # 背景满屏
        screen.blit(gameimage, (0, 0))  # 对其下标
        fClock.tick(FPS)
        # music.play()#播放音乐
        key_pressed = pygame.key.get_pressed()
        # 人物移动
        if key_pressed[K_w] or key_pressed[K_UP]:
            Human.now = 1  # 改变方向的值
            human.move_up()
        if key_pressed[K_s] or key_pressed[K_DOWN]:
            Human.now = 3  # 改变方向的值
            human.move_down()
        if key_pressed[K_a] or key_pressed[K_LEFT]:
            Human.now = 2  # 改变方向的值
            human.move_left()
        if key_pressed[K_d] or key_pressed[K_RIGHT]:
            Human.now = 0  # 改变方向的值
            human.move_right()

        # #人物和敌人相撞，结束游戏
        # if pygame.sprite.spritecollideany(human,enemy_sprites) is not None:
        #     print("GAME OVER")
        Human.last = Human.now
        #     running = False

        # 判断敌人数量是否少于最小敌人数量
        if len(enemy_sprites) < enemymincount:
            initenemy(enemymincount - len(enemy_sprites))

        # 发射子弹
        if key_pressed[K_j]:
            if not (Human.last == 3 or Human.now == 3 or Human.last == 1 or Human.now == 1):
                bullet = Bullet.new_bullet()  # 实例化子弹
                human.fireB()
                if bullet:  # 如果满足时间间隔
                    bullet.direction = Human.last  # 确定子弹移动的方向
                    bullet.fun1((human.rect.x, human.rect.y))
                    bullet_sprites.add(bullet)  # 添加 子弹
                    all_sprites.add(bullet)  # 添加子 弹
                    bullet.soundS()

        # 子弹击中敌人
        bullet_collide_dic = pygame.sprite.groupcollide(bullet_sprites, enemy_sprites, True, True)
        # 击中敌人，加分
        for i in bullet_collide_dic:
            Enemy.musicb()
            score += 1
        enemy_sprites.update()  # 更新游戏角色
        bullet_sprites.update()  # 更新子弹

        # # 转向
        # if key_pressed[K_SPACE]:
        #     human.turn_around()

        # 记录生命
        human.scrike(enemy_sprites)
        # 判断是否存活
        if not human.is_survive():
            live = False

        # 更新分数
        showinfo('SCORE:' + str(score), WHITE, (width - 200, 0), 30)
        # 更新生命值
        showinfo('LIFE:' + str(human.life), RED, (width - 200, 34), 30)
        all_sprites.draw(screen)  # 渲染所有角色
        pygame.display.flip()  # 更新游戏画面
    else:
        showinfo('GAME OVER', YELLOW, (width / 2, height / 2), 30)
        pygame.display.flip()  # 更新游戏画面
